import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xiaomi_shop/app/api/api.dart';
import 'package:xiaomi_shop/app/services/userService.dart';
import 'package:xiaomi_shop/app/services/xhttp.dart';
import 'package:xiaomi_shop/models/messageModel.dart';

class PhoneSmsLoginStep2Controller extends GetxController {
  final TextEditingController textEditingController = TextEditingController();
  final String phone = Get.arguments["tel"] ?? "";
  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  Future<MessageModel> validateLoginCode() async {
    var response = await XHttp.getInstance().post(Api.validateLoginCode,
        {"tel": phone, "code": textEditingController.text});
    if (response != null) {
      Result result = response as Result;
      if (result.data["success"]) {
        List userinfos = result.data["userinfo"];
        UserService.saveUser(userinfos.first);
        return MessageModel(success: true, message: result.data["message"]);
      } else {
        return MessageModel(success: false, message: result.data["message"]);
      }
    } else {
      return MessageModel(success: false, message: "网络错误");
    }
  }
}
